package com.craftworks.tiles.impl;

import com.craftworks.tiles.AbstractTile;
import io.github.parubok.text.multiline.MultilineLabel;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class TextTile extends AbstractTile
{
    private JLabel labelTitle;
    private MultilineLabel labelText;

    public static TextTile compact()
    {
        return new TextTile(Size.LANDSCAPE);
    }

    public static TextTile wide()
    {
        return new TextTile(Size.WIDE_LANDSCAPE);
    }

    public static TextTile large()
    {
        return new TextTile(Size.WIDE_PORTRAIT);
    }

    private TextTile(Size size)
    {
        super(size);
        initUI();
    }

    private void initUI()
    {
        labelTitle = new JLabel();
        labelTitle.setForeground(FOREGROUND);
        labelTitle.setFont(new Font("Source Han Sans CN", Font.PLAIN, 14));
        labelTitle.setBounds(4, 2, size.width - 8, 18);

        labelText = new MultilineLabel();
        labelText.setOpaque(false);
        labelText.setForeground(FOREGROUND);
        labelText.setUseCurrentWidthForPreferredSize(true);
        labelText.setLineSpacing(0.9f);
        labelText.setMaxLines(5);
        labelText.setFont(new Font("Source Han Sans CN", Font.PLAIN, 13));
        labelText.setBounds(4, 22, size.width - 8, size.height - 22);

        add(labelTitle);
        add(labelText);
    }

    @Override
    public List<String> propertyKeys()
    {
        return List.of("title", "text");
    }

    @Override
    public void setProperty(String key, Object value)
    {
        switch (key)
        {
            case "title" ->
            {
                switch (value)
                {
                    case null -> labelTitle.setText(null);
                    case String s -> labelTitle.setText(s);
                    default -> labelTitle.setText(value.toString());
                }
            }
            case "text" ->
            {
                switch (value)
                {
                    case null -> labelText.setText(null);
                    case String s -> labelText.setText(s);
                    default -> labelText.setText(value.toString());
                }
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    }
}
